﻿
using Redbrick.Silverlight.Common;
using System;
namespace Moria
{

    public class Point : Observable
    {
        int _x = 0;
        int _y = 0;

        public Point(int X = 0, int Y = 0) { this.x = X; this.y = Y; }

        public int x
        {
            get
            {
                return _x;
            }
            set
            {
                if (value == _x) return;
                _x = value;
                this.RaisePropertyChanged(() => this.x);
            }
        }
        public int y
        {
            get
            {
                return _y;
            }
            set
            {
                if (value == _y) return;
                _y = value;
                this.RaisePropertyChanged(() => this.y);
            }
        }

        protected override bool OnPropertyChanged(System.ComponentModel.PropertyChangedEventArgs args)
        {
            if (this.PointUpdated != null)
                this.PointUpdated.Invoke(this, new EventArgs());
            return base.OnPropertyChanged(args);
        }
        public override string ToString()
        {
            return "(" + this.x + "," + this.y + ")";
        }

        public int Distance(Point pt1, Point pt2)
        {
            int dy = pt1.y - pt2.y;
            if (dy < 0) dy = -dy;

            int dx = pt1.x - pt2.x;
            if (dx < 0) dx = -dx;

            return ((((dy + dx) << 1) - (dy > dx ? dx : dy)) >> 1);

        }

        public override bool Equals(object obj) { return base.Equals(obj); }
        public override int GetHashCode() { return base.GetHashCode(); }

        public static bool operator ==(Point a, Point b)
        {
            if ((object)a == null && (object)b != null) return false;
            if ((object)b == null && (object)a != null) return false;
            return (a.x == b.x) && (a.y == b.y);
        }
        public static bool operator !=(Point a, Point b) { return !(a == b); }

        public event EventHandler<EventArgs> PointUpdated;
    }
}
